package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.cndragon.R;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.presenter.SendActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.util.APKVersionCodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SendActivity extends BaseActivity<SendActivityPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.tv_check)
    TextView tvCheck;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.et_send_account)
    EditText etSendAccount;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_send;
    }

    @Override
    public SendActivityPresenter getPresenter() {
        return new SendActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setRightButtonOnClickListener(view -> SendRecordActivity.start(this));
        tvVersion.setText(App.INSTANCE.getString(R.string.bottom1) + APKVersionCodeUtils.getVerName(this));
    }

    @Override
    protected void initData() {
        super.initData();
        tvVersion.setText(App.INSTANCE.getString(R.string.bottom1) + APKVersionCodeUtils.getVerName(this));
        ClickUtil.click(300, tvCheck, () -> {
            if (isEmpty(etAccount)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurushoukuanrenzhanghao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etAccount))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            mPresenter.checkUserMsg(getEtext(etAccount));
        });
        ClickUtil.click(300, btnSubmit, () -> {
            if (isEmpty(etAccount)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurushoukuanrenzhanghao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etAccount))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            if (isEmpty(etSendAccount)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhuanzengshuliang));
                return;
            }
            if (isEmpty(etPayPwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurujiaoyimima));
                return;
            }
            mPresenter.sendCrystal(SPUtil.get(SPConfig.ID, ""),
                    getEtext(etSendAccount),
                    getEtext(etAccount),
                    getEtext(etPayPwd));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SendActivity.class);
        context.startActivity(intent);
    }

    public void getUserMsgOK(UserData userData) {
        if (userData != null) {
            ImageUtil.setCircleImage(HttpConfig.BASE_URL + userData.getHeadUrl(), ivHead);
            tvName.setText(isEmpty(userData.getNickname()) ? App.INSTANCE.getString(R.string.appMorenyonghu) : userData.getNickname());
        }
    }

    public void sendOK() {
        etSendAccount.getText().clear();
        etPayPwd.getText().clear();
        SendRecordActivity.start(this);
    }
}
